import sys as _sys
import json as _json
import traceback as _traceback

_configFile = './config.json'
_mod = _sys.modules[__name__]


def __getattr__(name):
	return getattr(_root, name)


def __setattr__(key, val):
	setattr(_root, key, val)


def storeConfig():
	try:
		with open(_configFile, 'w') as f:
			f.write(_json.dumps(_root, ensure_ascii=False,
								sort_keys=True, indent=4, separators=(',', ': ')))
	except Exception:
		pass


class _DotContainer(dict):
	def __init__(self, *args, **kwargs):
		dict.__init__(self, *args, **kwargs)
		self.__dict__ = self

	def toDotContainer(data):
		if isinstance(data, dict):
			for k, v in data.items():
				if isinstance(v, dict):
					data[k] = _DotContainer(v)
					_DotContainer.toDotContainer(data[k])
		elif isinstance(data, list) or isinstance(data, tuple):
			for v in data:
				if isinstance(v, dict):
					v = _DotContainer(v)
					_DotContainer.toDotContainer(v)
		else:
			return data
		return _DotContainer(data)

	def __getattr__(self, name):
		self.__dict__[name] = _DotContainer()
		return self.__dict__[name]


try:
	with open(_configFile, 'r') as f:
		_dict = _json.loads(f.read())
		_root = _DotContainer.toDotContainer(_dict)
		# print('糖糖的设定集：', _root, flush = True)
except Exception as e:
	print('糖糖喵的设定集好像出了点小问题的说……', flush = True)
	print(_traceback.format_exc(), flush = True)
	_root = _DotContainer()
